package httpclient.demo;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author cyt
 * @create 2020-07-27 10:42
 */
public class Demo01 {

    String url = "http://127.0.0.1:8001/";
    CloseableHttpClient client = HttpClientBuilder.create().build();
    /*   Ok httpclient */

    private OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(60,
            TimeUnit.SECONDS).build();

    @Test
    public void okClientCancel() throws IOException {
        Request request = new Request.Builder()
                .url(url + "delete")
                .delete()
                .build();
        Call call = okHttpClient.newCall(request);
        Response execute = call.execute();

        long cntTime = System.currentTimeMillis();
        while (true) {
            if (System.currentTimeMillis() - cntTime > 1000) {
                call.cancel();
                System.out.println("request cancel");
                break;
            }
        }
        System.out.println(execute.body().string());
    }

    @Test
    public void okClientUpload() throws IOException {
        MultipartBody file = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", "C:\\Users\\Administrator\\Desktop\\AOP.pdf"
                        , RequestBody.create(MediaType.parse("multipart/form-data"),
                                new File("C:\\Users\\Administrator\\Desktop\\AOP.pdf")))
                .build();
        Request request = new Request.Builder()
                .url(url + "upload")
                .post(file)
                .build();
        Call call = okHttpClient.newCall(request);
        Response execute = call.execute();
        System.out.println(execute.body().string());
    }

    @Test
    public void okClientDelete() throws IOException {
        Request request = new Request.Builder()
                .url(url + "delete")
                .delete()
                .build();
        Call call = okHttpClient.newCall(request);
        Response execute = call.execute();
        System.out.println(execute.body().string());
    }

    @Test
    public void okClientPost() throws IOException {
        HashMap<String, Object> map = new HashMap<>(1);
        map.put("msgBody", "v1");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), JSONObject.toJSONString(map));
        Request request = new Request.Builder()
                .url(url + "post")
                .post(requestBody)
                .build();

        Call call = okHttpClient.newCall(request);
        Response execute = call.execute();
        System.out.println(execute.body().string());
    }

    @Test
    public void okClientGet() throws IOException {
        Request request = new Request.Builder()
                .url(url + "get")
                .get()
                .build();

        Call call = okHttpClient.newCall(request);
        Response execute = call.execute();
        System.out.println(execute.body().string());
    }

    /*   httpclient */

    @Test
    public void testPut() throws IOException {
        HttpPut httpPut = new HttpPut(url + "Put");
        HashMap<Object, Object> map = new HashMap<>(1);
        map.put("msgBody", "abstractclass.test post request");
        httpPut.setHeader("Content-Type", "application/json;charset=utf-8");
        CloseableHttpResponse execute = client.execute(httpPut);
        System.out.println(EntityUtils.toString(execute.getEntity()));

    }

    @Test
    public void testUpload() throws IOException {

        HttpPost httpPost = new HttpPost(url + "upload");

        File file = new File("C:\\Users\\Administrator\\Desktop\\AOP.pdf");

        FileBody body = new FileBody(file);

        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

        entityBuilder.addPart("file", body);
        HttpEntity build = entityBuilder.build();

        httpPost.setEntity(build);
        CloseableHttpResponse execute = client.execute(httpPost);
        System.out.println(EntityUtils.toString(execute.getEntity()));
    }

    @Test
    public void testDelete() throws IOException {
        HttpDelete httpDelete = new HttpDelete(url + "delete");
        CloseableHttpResponse execute = client.execute(httpDelete);
        System.out.println(EntityUtils.toString(execute.getEntity()));
    }

    @Test
    public void testGet() throws IOException {
        HttpGet httpGet = new HttpGet(url + "test");
        CloseableHttpResponse execute = client.execute(httpGet);

        HttpEntity entity = execute.getEntity();
        String s = EntityUtils.toString(entity);

        System.out.println(s);
    }

    @Test
    public void testPost() throws IOException {

        HttpPost httpPost = new HttpPost(url + "post");
        HashMap<Object, Object> map = new HashMap<>(1);
        map.put("msgBody", "abstractclass.test post request");
        httpPost.setHeader("Content-Type", "application/json;charset=utf-8");

        String jsonString = JSONObject.toJSONString(map);

        httpPost.setEntity(new StringEntity(jsonString, "UTF-8"));

        CloseableHttpResponse execute = client.execute(httpPost);

        String string = EntityUtils.toString(execute.getEntity());
        System.out.println(string);
    }
}
